﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.DAL
{
    public sealed class SysReportDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public SysReportDAL()
        {

        }

        public SysReportInfo GetReport(int seq)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            SysReportInfo info = new SysReportInfo();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT *"
                        + " FROM ICM_SYS_REPORT"
                        + " WHERE ICM_REPORT_SEQ = @seq";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    info.Name_th = (dr[0]["ICM_REPORT_NAME_TH"]).ToString();
                    info.Name_en = (dr[0]["ICM_REPORT_NAME_EN"]).ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public IList<SysReportInfo> GetReportName(string strFilter)
        {
            string strSQL;
            DataSet ds = null;
            DataRowCollection dr = null;
            IList<SysReportInfo> infoList = new List<SysReportInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@filter", strFilter);

                strSQL = "SELECT *"
                        + " FROM ICM_SYS_REPORT"
                        + " WHERE RECORD_STATUS <> @delete_status"
                            + " AND ICM_REPORT_FILTER = @filter";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                dr = ds.Tables[0].Rows;
                if (dr.Count > 0)
                {
                    for (int i = 0; i < dr.Count; i++)
                    {
                        SysReportInfo info = new SysReportInfo();
                        info.Seq = (Convert.IsDBNull(dr[i]["ICM_REPORT_SEQ"]) ? null : (Nullable<int>)(dr[i]["ICM_REPORT_SEQ"]));
                        info.Name_th = (dr[i]["ICM_REPORT_NAME_TH"]).ToString();
                        info.Name_en = (dr[i]["ICM_REPORT_NAME_EN"]).ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
                //return null;
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
    }
}
